package com.cade.agriculturemall.base;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.cade.agriculturemall.R;

/**
 * Created by Lobby on 2017/11/7.
 */


public abstract class BaseActivity<P extends IBasePresenter> extends FWActivity<P> {
    protected TextView tvToolbarTitle;

private ProgressDialog progressDialog ;
    @Override
    protected void onResume() {
        super.onResume();
//        设置屏幕转动
        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        } else {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
    }

    /**
     * 设置标题
     *
     * @param isOpenBack true 为打开返回上一级的按钮
     */
    public void setupToolbar(boolean isOpenBack) {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        tvToolbarTitle = (TextView) findViewById(R.id.toolbar_title);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        getSupportActionBar().setDisplayHomeAsUpEnabled(isOpenBack);
    }


    @Override
    protected void onTitleChanged(CharSequence title, int color) {
        super.onTitleChanged(title, color);
        if (tvToolbarTitle != null) {
            tvToolbarTitle.setText(title);
        }
    }

    @Override
    public void showProgress() {
        showProgress("");
    }

    @Override
    public void showProgress(String msg) {
        if (progressDialog == null) {
            progressDialog = new ProgressDialog(BaseActivity.this);
            progressDialog.setCanceledOnTouchOutside(false);
            progressDialog.setMessage(msg);
            progressDialog.show();
        }
    }

    @Override
    public void showProgress(String msg, int progress) {
        if (progressDialog == null) {
            progressDialog.setMessage(msg);
            progressDialog.setProgress(progress);
            progressDialog.setMax(100);
            if (!progressDialog.isShowing())
                progressDialog.show();
        }
    }

    @Override
    public void dismissProgress() {
        if (progressDialog != null && progressDialog.isShowing())
            this.progressDialog.dismiss();
    }

    @Override
    public void showError(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showSuccess(String msg) {

    }

    @Override
    public void showMessage(String msg) {

    }

    @Override
    public void goBack() {
        finish();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }

    /**
     * 设置屏幕只能竖屏
     *
     * @param activity activity
     */
    public void setActivityState(Activity activity) {
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }

    //    是否允许屏幕旋转
    public void setScreenRoate(boolean isAllowScreenRoate) {
        this.isAllowScreenRoate = isAllowScreenRoate;
    }


//    public int getUid() {
//        if (UserManager().getUid() != null) {
//            if (UserManager().getUid().equals("")) {
//                return 0 ;
//            }
//        }
//        return Integer.parseInt(UserManager().getUid()) ;
//    }


    /**
     * 显示短Toast
     *
     * @param context：Context
     * @param text：String
     */
    protected void showShortToastByString(Context context, String text) {
        Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
    }

    /**
     * 显示长Toast
     *
     * @param context：Context
     * @param text：String
     */
    protected void showLongToastByString(Context context, String text) {
        Toast.makeText(context, text, Toast.LENGTH_LONG).show();
    }


}
